//
//  ZYEmoticationToolbar.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/20.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit

enum ZYEmoticonToolBarButtonType: Int {
    //  默认
    case Normal = 1000
    //  emoji
    case Emoji = 1001
    //  浪小花
    case Lxh = 1002
}

class ZYEmoticationToolbar: UIStackView {

    private var selectEmoticonBtn: UIButton?
    // 点击表情按钮的闭包
    var didSelectEmoticoBtnClosure:( (type:ZYEmoticonToolBarButtonType) -> () )?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        // 布局方式
        axis = .Horizontal
        distribution = .FillEqually
        addChildBtn("默认", imageName: "compose_emotion_table_left", type: .Normal)
        addChildBtn("Emoji", imageName: "compose_emotion_table_mid", type: .Emoji)
        addChildBtn("浪小花", imageName: "compose_emotion_table_right", type: .Lxh)
 
    }

    
    private func addChildBtn(title:String, imageName:String, type:ZYEmoticonToolBarButtonType) {
        
        let btn = UIButton()
        btn.tag = type.rawValue
        btn.setTitle(title, forState: .Normal)
        btn .addTarget(self, action: "emoticonBtnClick:", forControlEvents: .TouchUpInside)
        btn.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: .Selected)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: .Selected)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
//      btn.adjustsImageWhenHighlighted = false
        
        // 初始化默认点击nomal
        if type == .Normal {
            selectEmoticonBtn?.selected = false
            btn.selected = true
            selectEmoticonBtn = btn
        }
        
        addArrangedSubview(btn)

    }
    
    
    @objc private func emoticonBtnClick(button:UIButton) {
        
        selectEmoticonBtn?.selected = false
        button.selected  = true
        selectEmoticonBtn = button
        let type = ZYEmoticonToolBarButtonType(rawValue: button.tag)!
        
        didSelectEmoticoBtnClosure?(type: type)
       
        
    }
    
    
    func selectEmoticonButtonWithSection(section: Int) {
        
        if section + 1000 == selectEmoticonBtn?.tag {
            
            return
            
        }
        
        let btn = viewWithTag(section + 1000) as! UIButton
        
        selectEmoticonBtn?.selected = false
        btn.selected = true
        selectEmoticonBtn = btn
        
        
        
    }
    
}
